package com.example.worldcup414;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;
import java.io.InputStream;
import java.io.Serializable;
import java.util.ArrayList;

public class TeamsActivity extends AppCompatActivity {

    ListView mLvTeams;
    ArrayList<Team> teams;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_teams);

        initData();
        initView();
        initEvent();


    }

    private void initEvent() {
        mLvTeams.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                                    int position, long id) {


                Team team = teams.get(position);
                Intent intent = new Intent(TeamsActivity.this,TeamDetailActivity.class);
                intent.putExtra("team", team);//
                startActivity(intent);
            }
        });
    }


    //将json文件中的数据，读取，并解析成ArrayList
    private void initData() {
        Log.e("initdata", "我执行了" );
        //将JSON文件读取到内存字符串中
        String jsonStr = getJsonStr();
        //解析成ArrayList
        teams = getTeams(jsonStr);
    }

    private ArrayList<Team> getTeams(String jsonStr) {
        Log.e("getTeams", "我执行了" );
        ArrayList<Team> teamArrayList= new ArrayList<>();
        try {
            //将字符串转换为json数组
            JSONArray jsonArray = new JSONArray(jsonStr);
            for (int i = 0; i < jsonArray.length(); i++) {

                //获取jsonObject
                JSONObject jsonObject = jsonArray.getJSONObject(i);

                Team team = new Team();
                team.setId( jsonObject.getString("id"));
                team.setGroup( jsonObject.getString("group"));
                team.setName( jsonObject.getString("name"));
                team.setSheng( jsonObject.getInt("sheng"));
                team.setPing( jsonObject.getInt("ping"));
                team.setFu( jsonObject.getInt("fu"));
                team.setWingoal( jsonObject.getInt("wingoal"));
                team.setLosegoal( jsonObject.getInt("losegoal"));
                team.setTotalgoal( jsonObject.getInt("totalgoal"));
                team.setScore( jsonObject.getInt("score"));
                team.setPic( jsonObject.getString("pic"));
                team.setDetail( jsonObject.getString("detail"));

                teamArrayList.add(team);
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }

        return teamArrayList;
    }


    //读取并返回json字符串
    private String getJsonStr() {
        Log.e("getSring", "我执行了" );
        //或teams.json文件的读取字节输入流
        InputStream inputStream = getResources().openRawResource(R.raw.teams);
        byte[] bytes = new byte[102400];//定义一个足长的字节数组，保存读取到的字节信息
        try {
            int len = inputStream.read(bytes);
            String str = new String(bytes,0,len);
            return str;
        } catch (IOException e) {
            e.printStackTrace();
            return  null;
        }

    }

    private void initView() {
        Log.e("initview", "我执行了" );
        mLvTeams = findViewById(R.id.lv_teams);
        MyAdapter adapter =  new MyAdapter();
        mLvTeams.setAdapter(adapter);
    }
    class MyAdapter extends BaseAdapter{

        //???
        @Override
        public int getCount() {//确定ListView需要显示的项数
            return teams.size();
        }



        @Override
        public Object getItem(int position) {//确定第position位置显示的数据是什么
            return teams.get(position);
        }

        @Override
        public long getItemId(int position) {//
            return 0;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            //每一项 该如何显示
            View view =View.inflate(TeamsActivity.this,R.layout.list_item_layout,null);

            TextView tvGroupId = view.findViewById(R.id.tv_group_id);
            tvGroupId.setText(teams.get(position).getId());
            //TODO
            ImageView ivPic = view.findViewById(R.id.iv_team);
            String pic = teams.get(position).getPic();//图片名称

            int mipmap = getResources().getIdentifier(pic,"mipmap",getPackageName());
            ivPic.setImageResource(mipmap);
            return view;

        }
    }
}
